﻿#region Copyright
//Copyright 2012 Phelon Aka. Jon H.
/*
    This file is part of BGBuddy - Ultimate PVP Suite.

    BGBuddy - Ultimate PVP Suite is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BGBuddy - Ultimate PVP Suite is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Styx.Plugins;
using Styx;
using Styx.Helpers;
using Styx.CommonBot.POI;
using Styx.WoWInternals.WoWObjects;
using Styx.WoWInternals;
using Styx.Pathing;
using Styx.CommonBot;

namespace UltimatePvPSuite
{
    public static class RacialsAndTrinkets
    {
        //private static readonly Stopwatch TrinketTimer = new Stopwatch();
        public static string LuaGetReturnValueString(string lua) { return Lua.GetReturnValues(lua, "stuff.lua")[0]; }

        public static bool Pulse()
        {
            if (BreakCrowdControl())
                return true;
            return false;

        }

        public static bool BreakCrowdControl()
        {
            if (UltimatePvPSuite.EnableRacialTraits)
            {
                if (CanTrinket && SpellManager.CanCast("Every Man for Himself"))
                {
                    Logger.Write("Trying to break " + CanTrinket.ToString() + " with Every Man for Himself");
                    SpellManager.Cast("Every Man for Himself");
                    return true;
                }
                else if (EscapeArtist && SpellManager.CanCast("Escape Artist"))
                {
                    Logger.Write("Escape Artist");
                    SpellManager.Cast("Escape Artist");
                    return true;
                }
                else if (WilloftheForsaken && SpellManager.CanCast("Will of the Forsaken"))
                {
                    Logger.Write("Trying to remove " + WilloftheForsaken.ToString() + " with Will of the Forsaken!");
                    SpellManager.Cast("Will of the Forsaken");
                    return true;
                }
                else if (Stoneskin && SpellManager.CanCast("Stoneskin"))
                {
                    Logger.Write("Trying to remove: " + Stoneskin.ToString() + " using Stoneskin!");
                    SpellManager.Cast("Stoneskin");
                    return true;
                }
            }

            if (UltimatePvPSuite.EnablePvPTrinket)
            {
                    if (CanTrinket && HasFirstTrinket() && StyxWoW.Me.Inventory.Equipped.Trinket1.Cooldown == 0)
                    {
                        Logger.Write("Using Trinket: " + StyxWoW.Me.Inventory.Equipped.Trinket1.Name.ToString() + " to break " + CanTrinket.ToString());
                        StyxWoW.Me.Inventory.Equipped.Trinket1.Use();
                        return true;
                    }
                    else if (CanTrinket && HasSecondTrinket() && StyxWoW.Me.Inventory.Equipped.Trinket2.Cooldown == 0)
                    {
                        Logger.Write("Using Trinket: " + StyxWoW.Me.Inventory.Equipped.Trinket2.Name.ToString() + " to break " + CanTrinket.ToString());
                        StyxWoW.Me.Inventory.Equipped.Trinket2.Use();
                        return true;
                    }
            }
            return false;
        }

        #region Spell Breaks
        private static bool CanTrinket
        {
            get
            {
                return StyxWoW.Me.ActiveAuras.Any(a =>
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Asleep ||
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Banished ||
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Charmed ||
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Disoriented ||
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Fleeing ||
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Frozen ||
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Horrified ||
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Incapacitated ||
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Polymorphed ||
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Rooted ||
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Shackled ||
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Snared ||
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Stunned
                );
            }
        }

        private static bool Stoneskin
        {
            get
            {
                return StyxWoW.Me.ActiveAuras.Any(a =>
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Bleeding ||
                    a.Value.Spell.DispelType == WoWDispelType.Poison ||
                    a.Value.Spell.DispelType == WoWDispelType.Disease
                );
            }
        }

        private static bool EscapeArtist
        {
            get
            {
                return StyxWoW.Me.ActiveAuras.Any(a =>
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Infected ||
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Dazed ||
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Rooted ||
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Slowed ||
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Shackled
                );
            }
        }

        private static bool WilloftheForsaken
        {
            get
            {
                return StyxWoW.Me.ActiveAuras.Any(a =>
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Charmed ||
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Fleeing ||
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Horrified ||
                    a.Value.Spell.Mechanic == WoWSpellMechanic.Asleep
                );
            }
        }
        #endregion

        #region Trinket Checks
        private static string[] Trinkets = { "of the Horde", "of the Alliance", "Medallion of Tenacity", "Medallion of Meditation", 
                                               "Medallion of Cruelty", "Medallion of Prowess", "Medallion of Accuracy", "of the Scourge"  };
        public static bool HasFirstTrinket()
        {
            foreach (string trinket in Trinkets)
            {
                if (trinket != null)
                {
                    if (StyxWoW.Me.Inventory.Equipped.Trinket1 != null)
                    {
                        if (StyxWoW.Me.Inventory.Equipped.Trinket1.Name.Contains(trinket))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public static bool HasSecondTrinket()
        {
            foreach (string trinket in Trinkets)
            {
                if (trinket != null)
                {
                    if (StyxWoW.Me.Inventory.Equipped.Trinket1 != null)
                    {
                        if (StyxWoW.Me.Inventory.Equipped.Trinket2.Name.Contains(trinket))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
        #endregion
    }
}
